package com.mediatecture.flex.component.OpenSaveHelper
{
	import Model.Project;
	
	import com.mediatecture.flex.component.CustimXMLEncoder;
	
	import flash.desktop.Icon;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.xml.XMLDocument;
	
	import mx.core.FlexGlobals;
	import mx.rpc.xml.SimpleXMLEncoder;
	
	import spark.primitives.BitmapImage;

	public class XMLSaver
	{
		private var xmlDocument:XMLDocument;
		private var xmlEncoder:CustimXMLEncoder;
		
		public function XMLSaver()
		{
			
		}
		
		public function saveProject(file:File):void
		{
			this.xmlDocument = new XMLDocument();
			this.xmlEncoder = new CustimXMLEncoder(this.xmlDocument);
			file.addEventListener(Event.SELECT, save);
			var qName:QName = new QName("ISOVERDesignerProject");
			FlexGlobals.topLevelApplication.project.toXML(qName,this.xmlDocument, this.xmlEncoder);
			
			var xml:XML = new XML(this.xmlDocument);
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(xml.toString());
			stream.close();

		}
		
		public function saveAsProject(file:File):void
		{
			this.xmlDocument = new XMLDocument();
			this.xmlEncoder = new CustimXMLEncoder(this.xmlDocument);
			file.addEventListener(Event.SELECT, save);
			var qName:QName = new QName("ISOVERDesignerProject");
			FlexGlobals.topLevelApplication.project.toXML(qName,this.xmlDocument, this.xmlEncoder);
			file.browseForSave("Save");
		}
		
		
		protected function save(event:Event):void
		{
			var xml:XML = new XML(this.xmlDocument);
			var stream:FileStream = new FileStream();
			stream.open((event.currentTarget) as File, FileMode.WRITE);
			stream.writeUTFBytes(xml.toString());
			stream.close();
		}
	}
}